#include <iostream>
#include <fstream>
#include "pub_defs.h"
#include <sys/time.h>

// void print_arr(uint8_t* arr, const int m, const int n) {
//     std::cout<< "======begin=======" << std::endl;
//     for (int i = 1; i < m+1; i++) {
//         for (int j = 1; j < n+1; j++) {
//             std::cout <<  int(VA(arr, i, j, n)) << " ";
//         }
//         std::cout << std::endl;
//     }
//     std::cout<<"======end======="<<std::endl;
// }

void print_arr(uint8_t* arr, int m, int n){
    std::cout<< "======begin=======" << std::endl;
    for (int i=0;i<m;i++){
        for (int j=0;j<n;j++){
            std::cout <<  int(arr[i*n+j]) << " ";
        }
        std::cout << std::endl;
    }
    std::cout<<"======end======="<<std::endl;
}

/**
 * @brief 写入二进制文件
 */
template<typename T>
int write_binfile(T* data,  int n, const char* filepath) {
    std::ofstream infile(filepath, std::ios::binary);
    if (!infile) {
        return 1;
    }
    infile.write(reinterpret_cast<const char*>(data), n * sizeof(T));
    infile.close();
    return 0;
}
template int write_binfile<uint8_t>(uint8_t* data, int n, const char* filepath);

template<typename T>
int read_binfile(T* data, int n, const char* filepath) {
    std::ifstream infile(filepath, std::ios::binary);
    if (!infile) {
        return 1;
    }
    infile.read(reinterpret_cast<char*>(data), n * sizeof(T));
    infile.close();
    return 0;
}
template int read_binfile<uint8_t>(uint8_t* data, int n, const char* filepath);

template<typename T>
bool compare_array(T* a, T* b, int n) {
    for (int i = 0; i < n; i++) {
        if (a[i]==b[i]){
            continue;
        }
        return false;
    }
    return true;
}
template bool compare_array<uint8_t>(uint8_t* a, uint8_t* b, int n);

double wall_time() {
    struct timeval tv;
    gettimeofday(&tv, nullptr);
    return tv.tv_sec + 1e-6*tv.tv_usec;
}